import {
  $,
  request,
  DomainSelectAutoComplete,
  ActionDialog,
  Notification,
  NamedPage,
  tpl,
  addPage,
} from "@hydrooj/ui-default";

const managemember_school = new NamedPage("managemember_school", () => {
  const selector: any = DomainSelectAutoComplete.getOrConstruct(
    $("#set-domain-expiring-dialog [name=domainId]")
  );
  // $(document).on(
  //   "click",
  //   '[id="set-domain-expiring-button"]',
  //   addMonthExpiring
  const addMonthsDiglog = new ActionDialog({
    $body: $(".dialog__body--set-domain-expiring > div"),
    onDispatch(a: any) {
      if (a === "ok" && selector.value() === null) {
        selector.focus();
        return false;
      }
      return true;
    },
  });
  // );
  async function addMonthExpiring() {
    const addMonths = await addMonthsDiglog.open();
    if (addMonths !== "ok") return;
    const id = selector.value()._id;
    const months = addMonthsDiglog.$dom.find('[name="expiring"]').val();
    console.log(id);
    console.log(months);
    if (!id) {
      Notification.error("至少选择一个学校");
      return;
    }
    if (months == 0 || months === "") {
      Notification.error("延续的月数不能为0或空");
      return;
    }
    try {
      await request.post("", {
        operation: "addmonth",
        id,
        months,
      });
      window.location.reload();
    } catch (error) {
      Notification.error(error.message);
    }
  }
  $("#set-domain-expiring-button").click(() => addMonthExpiring());
});

addPage(managemember_school);
